package com.ruoyi.mushcoin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.mushcoin.domain.TgMessageNotice;
import com.ruoyi.mushcoin.domain.bo.TgMessageNoticeBo;
import com.ruoyi.mushcoin.domain.vo.TgMessageNoticeVo;
import com.ruoyi.mushcoin.mapper.TgMessageNoticeMapper;
import com.ruoyi.mushcoin.service.ITgMessageNoticeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static com.ruoyi.mushcoin.domain.table.TgMessageNoticeTableDef.TG_MESSAGE_NOTICE;

/**
 * TG消息通知Service业务层处理
 *
 * @author billy
 * 2025-03-05
 */
@Service
public class TgMessageNoticeServiceImpl extends BaseServiceImpl<TgMessageNoticeMapper, TgMessageNotice> implements ITgMessageNoticeService {
    @Resource
    private TgMessageNoticeMapper tgMessageNoticeMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(TG_MESSAGE_NOTICE);
    }

    private QueryWrapper buildQueryWrapper(TgMessageNoticeBo tgMessageNoticeBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TG_MESSAGE_NOTICE.STATUS.eq(tgMessageNoticeBo.getStatus()));

        return queryWrapper;
    }

    /**
     * 查询TG消息通知
     *
     * @param id TG消息通知主键
     * @return TG消息通知
     */
    @Override
    public TgMessageNoticeVo selectById(Long id) {
        return this.getOneAs(query().where(TG_MESSAGE_NOTICE.ID.eq(id)), TgMessageNoticeVo.class);

    }

    /**
     * 查询TG消息通知列表
     *
     * @param tgMessageNoticeBo TG消息通知Bo
     * @return TG消息通知集合
     */
    @Override
    public List<TgMessageNoticeVo> selectList(TgMessageNoticeBo tgMessageNoticeBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgMessageNoticeBo);
        return this.listAs(queryWrapper, TgMessageNoticeVo.class);
    }

    /**
     * 分页查询TG消息通知列表
     *
     * @param tgMessageNoticeBo TG消息通知Bo
     * @return 分页TG消息通知集合
     */
    @Override
    public TableDataInfo<TgMessageNoticeVo> selectPage(TgMessageNoticeBo tgMessageNoticeBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgMessageNoticeBo);
        Page<TgMessageNoticeVo> page = this.pageAs(PageQuery.build(), queryWrapper, TgMessageNoticeVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增TG消息通知
     *
     * @param tgMessageNoticeBo TG消息通知Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TgMessageNoticeBo tgMessageNoticeBo) {
        TgMessageNotice tgMessageNotice = MapstructUtils.convert(tgMessageNoticeBo, TgMessageNotice.class);


        return this.save(tgMessageNotice);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增TG消息通知，前台提供主键值，一般用于导入的场合
     *
     * @param tgMessageNoticeBo TG消息通知Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TgMessageNoticeBo tgMessageNoticeBo) {
        TgMessageNotice tgMessageNotice = MapstructUtils.convert(tgMessageNoticeBo, TgMessageNotice.class);


        return tgMessageNoticeMapper.insertWithPk(tgMessageNotice) > 0;//前台传来主键值
    }

    /**
     * 修改TG消息通知
     *
     * @param tgMessageNoticeBo TG消息通知Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TgMessageNoticeBo tgMessageNoticeBo) {
        TgMessageNotice tgMessageNotice = MapstructUtils.convert(tgMessageNoticeBo, TgMessageNotice.class);
        if (ObjectUtil.isNotNull(tgMessageNotice) && ObjectUtil.isNotNull(tgMessageNotice.getId())) {
            boolean updated = this.updateById(tgMessageNotice);
            return updated;
        }
        return false;
    }


    /**
     * 批量删除TG消息通知
     *
     * @param ids 需要删除的TG消息通知主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
